package com.cn.aam.checaiduo.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;

/**
 * author: leo on 2016/9/12 0012/15:57
 * email : leocheung4ever@gmail.com
 * description:
 * what & why is modified:
 */
public class AppInfoUtil {

    /**
     * 获取渠道号
     *
     * @param context  上下文环境
     * @param metaName manifest meta data
     * @return 渠道号
     */
    public static int getProductFlavorId(Context context, String metaName) {
        ApplicationInfo ai = null;
        try {
            ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        assert ai != null;
        Bundle bundle = ai.metaData;

        return bundle.getInt(metaName);
    }

    /**
     * 获取渠道名称
     *
     * @param context 上下文环境
     * @return 渠道名称
     */
    public static String getChannelName(Context context) {
        if (context == null)
            return null;
        String channelName = null;

        try {
            PackageManager pm = context.getPackageManager();
            if (pm != null) {
                // 注意此处为ApplicationInfo 而不是 ActivityInfo,
                // 因为友盟设置的meta-data是在application标签中，而不是某activity标签中，所以用ApplicationInfo
                ApplicationInfo applicationInfo = pm.getApplicationInfo(context.getPackageName(),
                        PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        channelName = applicationInfo.metaData.getString("UMENG_CHANNEL_VALUE");
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return channelName;
    }

    /**
     * 获取渠道名称 - 代号
     *
     * @param context 上下文环境
     * @return 渠道名称
     */
    public static String getChannelId(Context context) {
        if (context == null)
            return null;
        String channelName = null;
        try {
            PackageManager pm = context.getPackageManager();
            if (pm != null) {
                // 注意此处为ApplicationInfo 而不是 ActivityInfo,
                // 因为友盟设置的meta-data是在application标签中，而不是某activity标签中，所以用ApplicationInfo
                ApplicationInfo applicationInfo = pm.getApplicationInfo(context.getPackageName(),
                        PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        channelName = applicationInfo.metaData.getString("UMENG_CHANNEL");
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
//        Logger.e("debug===>channel name: " + channelName);
//        Logger.e("debug===>channel code: " + dispatch(channelName));
        return dispatch(channelName);
    }

    private static String dispatch(String name) {
        if (name == null)
            return "";
        String channelCode = null;
        switch (name) {
            case "CheCaiDuoMarket": //悟空车网
                channelCode = "10";
                break;
            case "_360zhushou": //360手机助手
                channelCode = "11";
                break;
            case "Anzhishichang": //安智市场
                channelCode = "12";
                break;
            case "Baidushoujizhushou": //百度手机助手
                channelCode = "13";
                break;
            case "Huaweishichang": //华为市场
                channelCode = "14";
                break;
            case "Jifeng": //机锋网
                channelCode = "15";
                break;
            case "Lianxiangle": //联想乐商店
                channelCode = "16";
                break;
            case "Meizushichang": //魅族市场
                channelCode = "17";
                break;
            case "Mumayi": //木蚂蚁
                channelCode = "18";
                break;
            case "Nduoshichang": //N多市场
                channelCode = "19";
                break;
            case "OppoMarket": //Oppo市场
                channelCode = "20";
                break;
            case "Sougouzhushou": //搜狗手机助手
                channelCode = "21";
                break;
            case "Taobaozhushou": //PP助手(淘宝)
                channelCode = "22";
                break;
            case "TengxunYingyongbao": //应用宝(腾讯)
                channelCode = "23";
                break;
            case "Vivoshichang": //Vivo市场
                channelCode = "24";
                break;
            case "Wandoujia": //豌豆荚
                channelCode = "25";
                break;
            case "Xiaomishangdian": //小米应用商店
                channelCode = "26";
                break;
            case "Yingyonghui": //应用汇
                channelCode = "27";
                break;
            case "Zhongguoliantong": //中国联通沃商店
                channelCode = "28";
                break;
            case "Zhongguoyidong": //中国移动
                channelCode = "29";
                break;
            case "Firim": //版本升级托管渠道
                channelCode = "30";
                break;
        }
        return channelCode;
    }
}
